package org.example;

import org.example.model.Author;
import org.example.model.Book;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 作用: 避免空指针异常
 * @author shenguangyang
 */
public class OptionalTest {
    @Test
    public void test() {
//        Author author = getAuthor();
//        Optional<Author> optionalAuthor = Optional.ofNullable(author);
//        optionalAuthor.ifPresent(author1 -> {
//            System.out.println(author1.getAge());
//        });

        Optional<Author> optionalAuthor = getAuthorOptional();
        optionalAuthor.ifPresent(author1 -> {
            System.out.println(author1.getAge());
        });
    }

    /**
     * 安全消费值
     */
    @Test
    public void testConsumeValue() {
        Optional<Author> optionalAuthor = getAuthorOptional();
        optionalAuthor.ifPresent(author -> System.out.println(author.getName()));
    }

    /**
     * 安全获取值
     */
    @Test
    public void testGetValue() {
        Optional<Author> optionalAuthor = getAuthorOptional();
        Author author = optionalAuthor.orElseGet(Author::new);
        Author author1 = optionalAuthor.orElseThrow(() -> new RuntimeException("数据为空"));
        System.out.println(author1.getAge());
        System.out.println(author.getAge());
    }

    /**
     * 过滤数据
     */
    @Test
    public void testFilterData() {
        Optional<Author> optionalAuthor = getAuthorOptional();
        Author author1 = optionalAuthor
                .filter(author -> author.getAge() > 18)
                .orElseGet(Author::new);
        System.out.println(author1.getAge());
    }

    @Test
    public void testIsPresent() {
        Optional<Author> optionalAuthor = getAuthorOptional();
        Optional<Author> optionalAuthor1 = optionalAuthor
                .filter(author -> author.getAge() > 18);
        if (optionalAuthor1.isPresent()) {
            System.out.println(optionalAuthor1.get().getName());
        }
    }

    /**
     * 数据转换
     */
    @Test
    public void testMap() {
        Optional<Author> optionalAuthor = getAuthorOptional();
        optionalAuthor
                .filter(author -> author.getAge() > 2)
                .map(author -> author.getAge() + 10)
                .ifPresent(System.out::println);
    }

    // 初始化一些数据
    private static Author getAuthor() {
        Author author1 = new Author(1L, "杨杰炜", "my introduction 1", 18, null);
        List<Book> books1 = new ArrayList<>();
        // 上面是作者和书
        books1.add(new Book(1L, "类别,分类啊", "书名1", 45D, "这是简介哦"));
        books1.add(new Book(2L, "高效", "书名2", 84D, "这是简介哦"));
        books1.add(new Book(3L, "喜剧", "书名3", 83D, "这是简介哦"));

        author1.setBooks(books1);
        return author1;
    }

    private static Optional<Author> getAuthorOptional() {
        Author author1 = new Author(1L, "杨杰炜", "my introduction 1", 18, null);
        List<Book> books1 = new ArrayList<>();
        // 上面是作者和书
        books1.add(new Book(1L, "类别,分类啊", "书名1", 45D, "这是简介哦"));
        books1.add(new Book(2L, "高效", "书名2", 84D, "这是简介哦"));
        books1.add(new Book(3L, "喜剧", "书名3", 83D, "这是简介哦"));

        author1.setBooks(books1);
        return Optional.ofNullable(author1);
    }
}
